load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
    name = "blake3",
    srcs = ["blake3.c"],
    visibility = ["//visibility:public"],
    deps = [
        ":blake3_dispatch",
        ":blake3_impl",
    ],
)

cc_library(
    name = "blake3_impl",
    hdrs = [
        "blake3.h",
        "blake3_impl.h",
    ],
)

cc_library(
    name = "blake3_dispatch",
    srcs = ["blake3_dispatch.c"],
    deps = [
        ":blake3_impl",
        ":blake3_portable",
    ] + select({
        "@platforms//cpu:aarch64": [":blake3_neon"],
        "@platforms//cpu:x86_32": [
            ":blake3_avx2",
            ":blake3_avx512",
            ":blake3_sse2",
            ":blake3_sse41",
        ],
        "@platforms//cpu:x86_64": [
            ":blake3_avx2_x86-64",
            ":blake3_avx512_x86-64",
            ":blake3_sse2_x86-64",
            ":blake3_sse41_x86-64",
        ],
        "//conditions:default": [],
    }),
)

cc_library(
    name = "blake3_portable",
    srcs = ["blake3_portable.c"],
    deps = [":blake3_impl"],
)

cc_library(
    name = "blake3_sse2",
    srcs = ["blake3_sse2.c"],
    copts = select({
        "@platforms//os:windows": ["/arch:SSE2"],
        "//conditions:default": ["-msse2"],
    }),
    deps = [":blake3_impl"],
)

cc_library(
    name = "blake3_sse2_x86-64",
    srcs = select({
        "@platforms//os:windows": ["blake3_sse2_x86-64_windows_msvc.asm"],
        "//conditions:default": ["blake3_sse2_x86-64_unix.S"],
    }),
)

cc_library(
    name = "blake3_sse41",
    srcs = ["blake3_sse41.c"],
    copts = select({
        # MSVC has no dedicated sse4.1 flag (see https://learn.microsoft.com/en-us/cpp/build/reference/arch-x86?view=msvc-170)
        "@platforms//os:windows": ["/arch:AVX"],
        "//conditions:default": ["-msse4.1"],
    }),
    deps = [":blake3_impl"],
)

cc_library(
    name = "blake3_sse41_x86-64",
    srcs = select({
        "@platforms//os:windows": ["blake3_sse41_x86-64_windows_msvc.asm"],
        "//conditions:default": ["blake3_sse41_x86-64_unix.S"],
    }),
)

cc_library(
    name = "blake3_avx2",
    srcs = ["blake3_avx2.c"],
    copts = select({
        "@platforms//os:windows": ["/arch:AVX2"],
        "//conditions:default": ["-mavx2"],
    }),
    deps = [":blake3_impl"],
)

cc_library(
    name = "blake3_avx2_x86-64",
    srcs = select({
        "@platforms//os:windows": ["blake3_avx2_x86-64_windows_msvc.asm"],
        "//conditions:default": ["blake3_avx2_x86-64_unix.S"],
    }),
)

cc_library(
    name = "blake3_avx512",
    srcs = ["blake3_avx512.c"],
    copts = select({
        "@platforms//os:windows": ["/arch:AVX512"],
        "//conditions:default": [
            "-mavx512f",
            "-mavx512vl",
        ],
    }),
    deps = [":blake3_impl"],
)

cc_library(
    name = "blake3_avx512_x86-64",
    srcs = select({
        "@platforms//os:windows": ["blake3_avx512_x86-64_windows_msvc.asm"],
        "//conditions:default": ["blake3_avx512_x86-64_unix.S"],
    }),
)

cc_library(
    name = "blake3_neon",
    srcs = ["blake3_neon.c"],
    deps = [":blake3_impl"],
)
